package com.open.capacity.finance.task;

import com.open.capacity.finance.constant.SourceImportResult;
import com.open.capacity.finance.entity.SourceFileImport;
import com.open.capacity.finance.entity.SourceFinance;
import com.open.capacity.finance.entity.SourceGLXML;
import com.open.capacity.finance.entity.SourceSHAREXML;
import com.open.capacity.finance.service.*;
import com.open.capacity.user.model.SysFile;
import com.open.capacity.user.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.*;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @desc 定时将导入的数据存储到数据库
 */
@Component
@Slf4j
public class SourceDataSave2DbTask implements ScheduledOfTask {

    @Autowired
    private SourcePzService sourcePzService;

    @Autowired
    private SourcePzUnionService sourcePzUnionService;
    @Autowired
    private SourcePzPartyService sourcePzPartyService;

    @Autowired
    private SourceFileImportService importService;

    @Autowired
    private SourceBaseUsecarService usecarService;

    @Autowired
    private SourceBasePayService payService;

    @Autowired
    private SourceBaseBusinessPayService businessPayService;

    @Autowired
    private SourceBaseBudgetExcuteService budgetExcuteService;

    @Autowired
    private SourceBaseBudgetTotal3Service budgetTotal3Service;

    @Autowired
    private SourceBaseBudgetTotal12Service budgetTotal12Service;

    @Autowired
    private SourceBaseFinalaccountTotal1Service finalaccountTotal1Service;
    @Autowired
    private SourceBaseFinalaccountTotal4Service finalaccountTotal4Service;
    @Autowired
    private SourceBaseFinalaccountTotal8Service finalaccountTotal8Service;

    @Autowired
    private SourceBaseHotelService hotelService;

    @Autowired
    private SourceBaseAssetService assetService;

    @Autowired
    private SourceBaseAssetTotalService assetTotalService;

    @Autowired
    private SysFileService fileService;

    @Autowired
    private TaskExecutor taskExecutor;
    Integer cronId = null;

    @Override
    public void setCronId(Integer cronId) {
           this.cronId = cronId;
    }

    @Override
    public void execute() {
        try {
            log.info("start excute SourceDataSave2DbTask");
            Map<String, Object> params = new HashMap<>();
            params.put("importProcess", 0);
            List<SourceFileImport> list = importService.findAll(params);


            //异步将离线文件写入数据库
            CompletableFuture.runAsync(() -> {
                list.forEach(data -> {
                    List<SysFile> glUrlList = fileService.findByFolderId(data.getGlXml());
                    String glUrl = glUrlList.size() == 0 ? "" : glUrlList.get(0).getUrl();

                    List<SysFile> shareUrlList = fileService.findByFolderId(data.getShareXml());
                    String shareUrl = shareUrlList.size() == 0 ? "" : shareUrlList.get(0).getUrl();

                    List<SysFile> unionxszUrlList = fileService.findByFolderId(data.getUnionxszXls());
                    String unionxszUrl = unionxszUrlList.size() == 0 ? "" : unionxszUrlList.get(0).getUrl();

                    List<SysFile> partyxszUrlList = fileService.findByFolderId(data.getPartyxszXls());
                    String partyxszUrl = partyxszUrlList.size() == 0 ? "" : partyxszUrlList.get(0).getUrl();

                    List<SysFile> usecarUrlList = fileService.findByFolderId(data.getUsecarXls());
                    String usecarUrl = usecarUrlList.size() == 0 ? "" : usecarUrlList.get(0).getUrl();

                    List<SysFile> paydataUrlList = fileService.findByFolderId(data.getPaydataXls());
                    String paydataUrl = paydataUrlList.size() == 0 ? "" : paydataUrlList.get(0).getUrl();

                    List<SysFile> budgetexcuteUrlList = fileService.findByFolderId(data.getBudgetexcuteXls());
                    String budgetexcuteUrl = budgetexcuteUrlList.size() == 0 ? "" : budgetexcuteUrlList.get(0).getUrl();

                    List<SysFile> budgettotalUrlList = fileService.findByFolderId(data.getBudgettotalXls());
                    String budgettotalUrl = budgettotalUrlList.size() == 0 ? "" : budgettotalUrlList.get(0).getUrl();

                    List<SysFile> hoteldataUrlList = fileService.findByFolderId(data.getHoteldataXls());
                    String hoteldataUrl = hoteldataUrlList.size() == 0 ? "" : hoteldataUrlList.get(0).getUrl();

                    List<SysFile> assetdataUrlList = fileService.findByFolderId(data.getAssetdataXls());
                    String assetdataUrl = assetdataUrlList.size() == 0 ? "" : assetdataUrlList.get(0).getUrl();

                    List<SysFile> assettotalUrlList = fileService.findByFolderId(data.getAssettotalXls());
                    String assettotalUrl = assettotalUrlList.size() == 0 ? "" : assettotalUrlList.get(0).getUrl();

                    List<SysFile> businesspaydataUrlList = fileService.findByFolderId(data.getBusinesspaydataXls());
                    String businesspaydataUrl = businesspaydataUrlList.size() == 0 ? "" : businesspaydataUrlList.get(0).getUrl();

                    List<SysFile> finalaccountUrlList = fileService.findByFolderId(data.getFinalaccountXls());
                    String finalaccountUrl = finalaccountUrlList.size() == 0 ? "" : finalaccountUrlList.get(0).getUrl();

                    try {
                        if (StringUtils.isNotEmpty(glUrl)) {
                            SourceGLXML glxml = sourcePzService.parseGL(data.getCompanyId(), data.getKjnd(), glUrl);
                            sourcePzService.saveGL2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), glxml);
                        }

                        if (StringUtils.isNotEmpty(shareUrl)) {
                            SourceSHAREXML sharexml = sourcePzService.parseSHARE(data.getCompanyId(), data.getKjnd(), shareUrl);
                            sourcePzService.saveSHARE2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), sharexml);
                        }

                        //工会序时账
                        if (StringUtils.isNotEmpty(unionxszUrl)) {
                            sourcePzUnionService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), unionxszUrl);
                        }

                        //党费序时账
                        if (StringUtils.isNotEmpty(partyxszUrl)) {
                            sourcePzPartyService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), partyxszUrl);
                        }

                        //公务用车
                        if (StringUtils.isNotEmpty(usecarUrl)) {
                            usecarService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), usecarUrl);
                        }

                        //支付数据
                        if (CollectionUtils.isNotEmpty(paydataUrlList)) {
                            payService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), paydataUrlList);
                        }

                        //预算执行情况
                        if (StringUtils.isNotEmpty(budgetexcuteUrl)) {
                            budgetExcuteService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgetexcuteUrl);
                        }

                        //预算收入支出总表
                        if (StringUtils.isNotEmpty(budgettotalUrl)) {
                            budgetTotal3Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgettotalUrl);
                            budgetTotal12Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgettotalUrl);
                        }

                        //会议酒店数据
                        if (StringUtils.isNotEmpty(hoteldataUrl)) {
                            hotelService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), hoteldataUrl);
                        }

                        //资产数据
                        if (StringUtils.isNotEmpty(assetdataUrl)) {
                            assetService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), assetdataUrl);
                        }

                        //资产汇总数据
                        if (StringUtils.isNotEmpty(assettotalUrl)) {
                            assetTotalService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), assettotalUrl);
                        }

                        //业务支付单据数据
                        if (CollectionUtils.isNotEmpty(businesspaydataUrlList)) {
                            businessPayService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), businesspaydataUrlList);
                        }

                        //部门决算数据
                        if (StringUtils.isNotEmpty(finalaccountUrl)) {
                            finalaccountTotal1Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                            finalaccountTotal4Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                            finalaccountTotal8Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                        }

                        data.setImportProcess(SourceImportResult.SUCCESS.getCode());
                        importService.update(data);
                    } catch (Exception e) {
                        data.setImportProcess(SourceImportResult.FAIL.getCode());
                        importService.update(data);
                        log.error(e.toString());
                    }
                });
            }, taskExecutor);

        } catch (Throwable e) {
            log.error("execute process has error(Throwable) : ", e);
        }
    }

    @Override
    public void trigger(String startTime) throws IOException {

    }
}
